package com.netty.pro.netty.nio;

import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author xuzn
 * @ClassName: NioFileChannel02
 * @ProjectName echo-project
 * @date 2020/3/4/21:51
 */
public class NioFileChannel02 {

  public static void main(String[] args) throws Exception {
    // 创建文件的输入流
    File file = new File("d:\\filechannel01.txt");
    // 将文件放入到输入流中
    FileInputStream fileInputStream = new FileInputStream(file);
    FileChannel fileChannel = fileInputStream.getChannel();
    // 创建buffer
    ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
    // 将通道的数据读到buffer
    fileChannel.read(byteBuffer);
    fileInputStream.close();
    System.out.println(new String(byteBuffer.array()));
  }
}
